<?php 
class IndexController extends DomController {
	
	public function index() {
		
	}
	
	
	public function _prepareLogin() {
		$post = DomFgPost::getInstance();
		if( $post->login ) {
			
			$result = AdminUser::loginByUsername( $_REQUEST['username'], $_REQUEST['password'] );
			
			if( 0 === $result ) {
				$this->goToUrl( $_SERVER['HTTP_REFERER'] );
			} else if( 100 == $result ) {
				$errorMessage = 'Kasutajat ei leitud';
			} else if( 101 == $result) {
				$errorMessage = 'Parool ei ole õige!';
			} else if( 1 == $result) {
				$errorMessage = 'Kasutaja ei ole aktiveeritud';
			} else if( 2 == $result) {
				$errorMessage = 'Kasutaja on blokeeritud!';
			} else {
				$errorMessage = 'Tundmatu viga nr ' . $result . '!';
			}
			
			$error = array(
				'type' => 'error',
				'message' => $errorMessage,
				'colspan' => 2,
			);
		}
		
		
		$panel = array(
			'title' => 'Administration',
			'layout' => 'table',
			'layoutConfig' => array(
				'columns' => 2,
			),
			'style' => 'width:470px',
			'action' => $this->getCurrentUrl(),
			'items' => array(
				$error,
				array(
					'type' => 'textField',
					'title' => 'Username',
					'name' => 'username',
					'value' => $post->username,
				),
				array(
					'type' => 'password',
					'title' => 'Password',
					'name' => 'password',				
				),
				array('type' => 'value',),
				array(
					'type' => 'button',
					'title' => 'Login',
					'name' => 'login',
				),
			),
		);
		
		
		$this->view->contents = DomFg::render( $panel );
	}
	
	public function _prepareloggedIn() {
		if( $_GET['logout'] ) {
			$this->getUser()->logout();
			$this->goToUrl( $this->getSiteUrl() );
		}
		
		$menu = array(
			'type' => 'menu',
			'items' => array(
				array(
					'title' => 'Admin',	
					'link' => $this->getSiteUrl(''),
				),
				array(
					'title' => 'Administrators',	
					'link' => $this->getSiteUrl('Users/users/'),
					'items' => array(
						array(
							'title' => 'Users',
							'link' => $this->getSiteUrl('Users/users/'),
						),
						array(
							'title' => 'Statistics',
							'link' => $this->getSiteUrl('Users/statistics/'),
						),
					),
				),
				array(
					'title' => 'Log out',
					'link' => $this->getSiteUrl('?logout=1'),
				),
			),
		);
	
		$this->view->menu = DomFg::render( $menu );
	}
	
	
	public function setup() {
		$this->view->metaAuthor = '';
		$this->view->metaKeywords = '';
		$this->view->metaDescription = '';
		$this->view->pageTitle = 'Admin';

		if( $user = AdminUser::getLoggedInUser() ) {
			$this->getSite()->setUser( $user );
			$this->_prepareloggedIn();
		} else {
			
			$location = $this->getSite()->getLocation();
			if( $location->controller != 'Index' or $location->controler == 'Index' && $location->method != 'index') {
				$this->auth();
			}
			
			$this->_prepareLogin();
		}
	}
	
	public function finalize() {
		$this->view->head = DomFg::renderHead();
		$this->view->header = DomFg::renderHeader();
		$this->view->footer = DomFg::renderFooter();
	}

	public function error404() {
		$this->view->addPartitioner('contents', 'Errors/error404.html');
	}
	
	public function error403() {
		$panel = array(
			'style' => 'width:470px',
			'type' => 'panel',
			'items' => array(
				array(
					'type' => 'warning',
					'message' => 'Antud lehe nägemiseks peate sisselogima!',
				),
			),
		);
		$this->view->addPartitioner('contents', 'Errors/error403.html');
		$this->view->error403 = DomFg::render( $panel );
		$this->_prepareLogin();
	}

}
?>